package com.example.Lezedata.ui.dataPage.subVc;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.Lezedata.R;
import com.example.Lezedata.dataManager.GBUserManager;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.util.GBTimeManager;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import bid.Bid;
import common.Common;

public class DataNodeList extends GBBaseVc {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_data_node_list;
    }

    @Override
    public CharSequence title() {
        return "节点列表";
    }
    private int pageStart = 0;
    private int pageSize = 20;
    private SmartRefreshLayout refreshLayout;
    private ListView listView;
    private TextView time;

    ArrayList<Bid.NodePool> pools = new ArrayList<>();
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        listView = findViewById(R.id.listView);
        time = findViewById(R.id.time);
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                loadData();
            }
        });
        time.setText(String.format("更新日期"));
        listView.setAdapter(adapter);
        hudShow();
        loadData();
    }

    void loadData(){
        int Version = getIntent().getIntExtra("version",0);
        Common.PageInfo pageInfo = Common.PageInfo.newBuilder().setPageStart(pageStart).setPageSize(pageSize).build();
        Bid.GetUserNodesRequest request = Bid.GetUserNodesRequest.newBuilder().setUserId(GBUserManager.getUserId()).setBeeVersion(bid.Bid.BeeVersion.forNumber(Version)).setPageInfo(pageInfo).build();
        GBNetRequest.stubUser().getUserNodes(request, ServerResponseObserver.observer(new ServerResponseObserver.requestListener<Bid.GetUserNodesResponse>() {
            @Override
            public void complete(Bid.GetUserNodesResponse value, GBError error) {
                refreshLayout.finishLoadMore();
                hudDismiss();
                if (error == null){
                    if (pageStart == 0){
                        if (value.getNodePoolsCount() > 0){
                            time.setText(String.format("更新日期:%s",GBTimeManager.timesYMD_HMS(value.getNodePools(0).getRefreshTime())));
                        }
                        pools.clear();
                    }
                    pools.addAll(value.getNodePoolsList());
                    if (value.getCount() >= pageSize){
                        pageStart++;
                    }
                    refreshLayout.setEnableLoadMore(value.getCount() >= pageSize);
                    adapter.notifyDataSetChanged();
                }
            }
        }));
    }

    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return pools.size();
        }

        @Override
        public Bid.NodePool getItem(int position) {
            return pools.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = DataNodeListCell.newInstance(mContext,listView);
            }
            DataNodeListCell cell = (DataNodeListCell)convertView;
            cell.setPool(getItem(position));
            return convertView;
        }
    };
}